<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button id="clientCaseBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="clientCaseBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="clientCaseTable" lay-filter="clientCaseTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="clientCaseTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="clientCaseForm">
    <form lay-filter="clientCaseForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">企业案例名称</label>
            <div class="layui-input-block">
                <input name="enterpriseName" placeholder="请输入企业案例名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">客户企业名称</label>
            <div class="layui-input-block">
                <input name="clientName" placeholder="请输入客户企业名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标题图片</label>
            <div class="layui-input-block">
                <div id="btnUpload1" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传图片</div>
                <div id="btnUpload1_path" style="margin-left: 260px;line-height: 40px;"></div>
                <input id="titleImg" name="titleImg" type="hidden"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">关键词</label>
            <div class="layui-input-block">
                <input name="antistop" placeholder="请输入关键词" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">一句话简介</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入一句话简介" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea id="describes" name="describes" placeholder="请输入内容(200字以内)" class="layui-textarea" maxlength="200" ></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="clientCaseFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin',
    'layedit', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var layedit = layui.layedit;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#clientCaseTable',
            url: config.base + 'enterprise/system/admin/clientCase/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                ,{field: 'enterpriseName',  title: '企业案例名称'}
                ,{field: 'clientName', title: '客户企业名称'}
                ,{field: 'titleImg',  title: '标题图片'
                    ,templet:'<div><img src="{{ d.titleImg}}" style="height: 50px;"></div>'}
                ,{field: 'antistop',  title: '关键词'}
                ,{field: 'describes',  title: '描述'}
                ,{field: 'createTime', sort: true, title: '创建时间'}
                ,{align: 'center', toolbar: '#clientCaseTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {

            }
        });

        // 工具条点击事件
        table.on('tool(clientCaseTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });


        //监听排序
        table.on('sort(clientCaseTable)', function(obj){
            table.reload('clientCaseTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#clientCaseBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '880px',
                offset: '65px',
                title: data ? '修改客户案例' : '添加客户案例',
                content: $('#clientCaseForm').html(),
                success: function () {
                    var layeditIndex14 = layedit.build('describes',{
                        uploadImage: {
                            url: config.upload_url + 'layuiUpload?access_token=' +config.getToken(),
                            type: 'post'
                        }
                    });
                    if(data){
                        layedit.setContent(layeditIndex14, data.describes,false);
                    }
                    //查询企业信息
                    var enterpriseInfo;
                    admin.req('enterprise/system/admin/enterpricceId/getEnterpricceIdVO', {}, function (res) {
                        enterpriseInfo = res.data;
                    }, 'get', false);
                    if ("" == enterpriseInfo || null == enterpriseInfo) {
                        layer.msg("企业信息获取异常", {icon: 1});
                        return;
                    }
                    upload.render({
                        elem: '#btnUpload1'
                        , url: config.upload_url + 'uploadFile'
                        , data: {"path": enterpriseInfo.enterpriseCode + "/"}
                        , accept: 'file'
                        , exts: 'jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx'
                        , headers: {Authorization: 'Bearer ' + config.getToken()}
                        , done: function (res) {
                            if (res.code == 200) {
                                //layer.msg(res.msg, {icon: 1});
                                var imagUrl = res.data.url;
                                $('#btnUpload1_path').html('<img src="' + imagUrl + '" style="height: 50px;" />');
                                $('#titleImg').val(imagUrl);
                            } else if (res.code == 502) {
                                return location.replace('admin/login.html');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });





                    form.val('clientCaseForm', data);
                    // 表单提交事件
                    form.on('submit(clientCaseFormSubmit)', function (d) {
                        layer.load(2);
                        d.field.describes = layedit.getContent(layeditIndex14);
                        admin.req(data ? 'enterprise/system/admin/clientCase/update' : 'enterprise/system/admin/clientCase/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('clientCaseTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/system/admin/clientCase/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#clientCaseBtnExport').click(function () {
            var checkRows = table.checkStatus('clientCaseTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>